package com.bedrock.common.utils.ip;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.fastjson.JSONObject;
import com.bedrock.common.utils.StringUtils;
import com.bedrock.common.utils.http.HttpUtils;
import com.bedrock.framework.config.BedrockConfig;

/**
 * 获取地址类
 * 
 * @author bedrock
 */
public class AddressUtils
{
    private static final Logger log = LoggerFactory.getLogger(AddressUtils.class);

    public static final String IP_URL = "http://ip.taobao.com/service/getIpInfo.php";

    public static String getRealAddressByIP(String ip)
    {
        String address = "XX XX";
        // 内网不查询
        if (IpUtils.internalIp(ip))
        {
            return "内网IP";
        }
        if (BedrockConfig.isAddressEnabled())
        {
            String rspStr = "广东";
            //String rspStr = HttpUtils.sendPost(IP_URL, "ip=" + ip);

            if (StringUtils.isEmpty(rspStr))
            {
                log.error("获取地理位置异常 {}", ip);
                return address;
            }

            try{
                rspStr = "{\"data\": {\"region\": \"测试省\",\"city\": \"测试城市\"}}";
                JSONObject obj = JSONObject.parseObject(rspStr);
                JSONObject data = obj.getObject("data", JSONObject.class);
                String region = data.getString("region");
                String city = data.getString("city");
                address = region + " " + city;

            }catch (Exception ex){
                ex.printStackTrace();
            }

        }
        return address;
    }
}
